<?php

namespace app\common\model;

use think\Request;
use think\Exception;
use think\Db;

/**
 * 
 * Class User
 * @package app\common\model
 */
class MemberInfo extends BaseModel
{
	protected $name = 'member_info';
   protected $autoWriteTimestamp = false;

   /**
   * 账户加钱
   * @param string $userId    用户id
   * @param $post['change']   变动金额
   * @param $post['details']  变动说明
   */
   public static function incrMoney($userId, $post)
   {
         //Member::getMemberInfo($userId);
         $memberObject = MemberInfo::get(['uid' => $userId]);
         if ($memberObject) {
            $money = $memberObject['money'] + $post['change'];
         } else {
            $memberObject = new self(); 
            $money = $post['change'];
            $memberObject->uid = $userId;
         }
         $memberObject->money = $money;
         if ($memberObject->save() && MemberMoneyLog::incrMoneyLog($post, $memberObject)) {
            return true;
         }
         throw new Exception('账户加钱失败');
   }

   /**
   * 账户扣钱
   * @param string $userId    用户id
   * @param $post['change']   变动金额
   * @param $post['details']  变动说明
   */
   public static function decrMoney($userId, $post)
   {
      //Member::getMemberInfo($userId);
      $memberObject = MemberInfo::get(['uid' => $userId]);
      if($memberObject == '' || ($memberObject['money'] < $post['change'])) {
         throw new Exception('没有账户余额');
      }
      $memberObject->money = $memberObject['money'] - $post['change'];
      if ($memberObject->save() && MemberMoneyLog::decrMoneyLog($post, $memberObject)) {
         return true;
      }
      throw new Exception('账户扣钱失败');
   }


   
}
